(function($) {
	// options = {
	// 	title, sizeClass, btnNames, isForm
	// }
	$.dfaceModal = function(options) {
		var modal = $('<div class="modal fade">' +
						'<div class="modal-dialog ' + (options.sizeClass || '') + '">' +
							'<div class="modal-content">' +
								'<div class="modal-header">' +
									'<button class="close"' + (options.notDismiss || ' data-dismiss="modal"') + '>' +
										'<span>&times;</span>' +
									'</button>' +
									'<h3 class="modal-title">' + (options.title || 'title') + '</h3>' +
								'</div>' +
								'<div class="modal-body" style="' + (options.content.style || '') + '"></div>' +
							'</div>'+
						'</div>' +
					'</div>');

		if (options.btns) {
			if (options.btns.length == 1) {
				modal.find('.modal-body').after(
					'<div class="modal-footer">' + 
						'<button class="btn btn-primary" data-dismiss="mdoal" id="btnOk">' + options.btns[0] + '</button>' +
					'</div>');
			} else if (options.btns.length == 2) {
				modal.find('.modal-body').after(
					'<div class="modal-footer">' + 
						'<button class="btn btn-warning" data-dismiss="mdoal" id="btnCancel">' + options.btns[0] + '</button>' +
						'<button class="btn btn-primary" id="btnOk">' + options.btns[1] + '</button>' +
					'</div>');
			}
			modal.find('.modal-footer button:first').on('click', function() {
				modal.modal('hide');
			});
		}

		// if (options.btnFile) {
			// modal.find('.modal-footer').append(
			// 		'<button class="btn btn-success pull-left" id="btnFile" style="position: relative; overflow: hidden;">' + 
			// 			'<span class="glyphicon glyphicon-plus"></span> ' + options.btnFile.name + 
			// 			'<input type="file" multiple="multiple" accept="' + options.btnFile.accept + 
			// 				'" style="position: absolute; top: 0; right: 0; min-width: 100%; min-height: 100%; opacity: 0; cursor: inherit;">' + 
			// 		'</button>');
			// modal.find('#btnOk').prop('disabled', true);

		// }

		if (typeof(options.content) === 'object') {
			options.content = options.content.value;
		}

		if (options.isForm) {
			modal.find('.modal-footer button:last').prop('disabled', true);
		    modal.find('.modal-body').append(options.content);
		    var form = modal.find('form'),
		    	textareaDiv = form.find('textarea'),
		    	fileInputDiv = form.find('input[type="file"]'),
		        disabledFn = function() {
				var disabled = false;
		        form.find('input[type="text"]').each(function() {
		            if ($(this).val().length == 0) {
		                disabled = true;
		            }
		        });
		        var textareaDiv = form.find('textarea');
		        if (textareaDiv.length != 0 && textareaDiv.val().length == 0) {
		            disabled = true;
		        }
		        modal.find('button:last').prop('disabled', disabled);
	    	};
		    form.on('keyup', disabledFn);
		    if (fileInputDiv.length != 0) {
			    fileInputDiv.on('change', function(e) {
			    	var fileName = e.target.files.length == 0 ? '' : e.target.files[0].name;
			    	$(this).parents('.form-group').find('input[type="text"]').val(fileName);
			    	disabledFn();
			    });
		    }
		} else {
		    modal.find('.modal-body').css('word-wrap', 'break-word').append(options.content);
		}

		// modal.on('show.bs.modal', function() {
		// 	if (options.isForm) {
		// 		modal.find('form')[0].reset();
		// 	}
		// 	if (modal.find('.alert')) {
		// 		modal.find('.alert').remove();
		// 	}
		// });

		modal.on('hidden.bs.modal', function() {
			modal.remove();
		})

		return {
			show: function() {
				modal.modal({
					backdrop: options.backdrop || true,
					keyboard: options.keyboard || true,
					show: true
				});
				return modal;
			}
		};
	}

	$.dfaceAlert = function(alertType, message) {
		return $('<div class="alert alert-' + alertType + ' alert-dismissible">' + 
					'<button class="close" data-dismiss="alert" aria-label="Close">' + 
						'<span aria-hidden="true">&times;</span></button>' + message + 
				'</div>');
	}

})( jQuery );